//
//  MTAlert.m
//  MThird
//
//  Created by mac on 2018/4/3.
//  Copyright © 2018年 gzx. All rights reserved.
//

#import "MTAlert.h"

@implementation MTAlert

+(void)showAlertWithTitle:(NSString *_Nullable)title  message:(NSString *_Nullable)message cancel:(NSString *_Nullable)cancel ok:(NSString *_Nullable)ok clickBlock:(void (^_Nullable)(NSInteger tag))clickBlock{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    if (cancel) {
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancel style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            if (clickBlock) {
                clickBlock(0);
            }
        }];
        [alertController addAction:cancelAction];
    }

    if (ok) {
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:ok style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if (clickBlock) {
                clickBlock(1);
            }
        }];
        [alertController addAction:okAction];
    }
    dispatch_async(dispatch_get_main_queue(), ^{
            [[MTAlert getCurrentVC] presentViewController:alertController animated:YES completion:nil];
    });

}
+(void)showSheetWithTitle:(NSString *_Nullable)title  message:(NSString *_Nullable)message cancel:(NSString *_Nullable)cancel ok:(NSArray *_Nullable)okArray clickBlock:(void (^_Nullable)(NSInteger tag))clickBlock{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
    if (cancel) {
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancel style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            if (clickBlock) {
                clickBlock(0);
            }
        }];
        [alertController addAction:cancelAction];
    }

    if (okArray) {
        for (NSInteger i = 0 ;i < okArray.count; i ++ ) {
            UIAlertAction *okAction = [UIAlertAction actionWithTitle:okArray[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                if (clickBlock) {
                    clickBlock(i + 1);
                }
            }];
            [alertController addAction:okAction];
        }
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        [[MTAlert getCurrentVC] presentViewController:alertController animated:YES completion:nil];
    });
}
+(UIViewController *)getCurrentVC
{
    UIViewController *result = nil;

    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows)
        {
            if (tmpWin.windowLevel == UIWindowLevelNormal)
            {
                window = tmpWin;
                break;
            }
        }
    }
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];

    if ([nextResponder isKindOfClass:[UIViewController class]])
        result = nextResponder;
    else
        result = window.rootViewController;

    return result;
}

@end
